package com.zpark.user_system.servlet;

import com.zpark.user_system.bean.User;
import com.zpark.user_system.utils.JDBCUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.*;

/**
 * 用户登录的servlet类
 */
@WebServlet("/user/user_login.do")
public class UserLoginServlet extends HttpServlet {
    /**
     * 登录业务：
     *      1、用户输入登录信息
     *      2、用户点击登录按钮
     *      3、后台获取用户登录数据
     *      4、校验数据
     *      5、根据登录名字查询数据
     *          如果查询有结果，则比较密码是否一致，一致则登录成功，否则，登录失败
     *          如果查询没有结果，则登录失败
     *      6、响应登录结果
     */
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取用户登录数据
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        // 定义用户登录信息
        String msg = "";

        // 根据用户名向数据库查询数据
        Connection conn = null;
        PreparedStatement stat = null;
        ResultSet res = null;

        try {
            // 获取数据库连接对象
            conn = JDBCUtils.getConnection();
            // 编写sql语句
            String sql = "select id, username, password, sex, language, city from user where username = ?";
            // 获取sql预编译对象
            stat = conn.prepareStatement(sql);
            // 替换占位符
            stat.setString(1, username);
            // 执行sql
            res = stat.executeQuery();
            User user = null;
            // 获取遍历结果集
            while (res.next()) {
                user = new User();
                user.setUsername(res.getString("username"));
                user.setId(res.getInt("id"));
                user.setPassword(res.getString("password"));
                user.setLanguage(res.getString("language"));
                user.setSex(res.getString("sex"));
                user.setCity(res.getString("city"));
            }

            // 判断查询结果是否为null
            if (user == null) {
                // 如果为null，则提示用户用户不存在，重新登录
                msg = "用户不存在";
                request.setAttribute("msg", msg);
                // 转发到登录页面
                request.getRequestDispatcher("/jsp/login.jsp").forward(request, response);
            }else{
                // 否则，对比密码
                if (!user.getPassword().equals(password)) {
                    // 如果密码错误，则提示用户，用户名或者密码错误
                    msg = "用户名或者密码错误";
                    request.setAttribute("msg", msg);
                    // 转发到登录页面
                    request.getRequestDispatcher("/jsp/login.jsp").forward(request, response);
                }else{
                    // 将用户独享绑定到session
                    HttpSession session = request.getSession();
                    session.setAttribute("user", user);
                    // 否则登录成功，跳转到登录成功页面
                    response.sendRedirect(request.getContextPath() + "/jsp/main.jsp");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭数据库连接
            JDBCUtils.close(conn, stat, res);
        }




    }
}
